home *** CD-ROM | disk | FTP | other *** search
Java Source | 2004-01-31 | 974 b | 34 lines |
- import java.lang.reflect.Constructor;
-
- class TestConstructor {
- public TestConstructor() {}
-
- private TestConstructor(int i) {}
-
- protected TestConstructor(long l, int i) {}
-
- public void sayHello() {
- System.out.println("Hello");
- }
-
-
- public static void main(String[] args) throws Exception {
- TestConstructor tc = new TestConstructor();
- System.out.println("Class name: " + tc.getClass().getName());
- Constructor[] c = tc.getClass().getDeclaredConstructors();
- System.out.println("Number of Constructors: " + c.length);
-
- for(int i=0; i<c.length; i++) {
- System.out.println("Method " + i + " modifiers: " + c[i].getModifiers() + " Number of Parameters: " + c[i].getParameterTypes().length);
-
- if(c[i].getParameterTypes().length == 0) {
- System.out.println("Now trying to instantiate:");
- Object[] ia = new Object[0];
- TestConstructor myTC = (TestConstructor)c[i].newInstance(ia);
- myTC.sayHello();
- }
- }
-
-
- }
- }